package org.example.backend.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.backend.dto.AnimalDTO;
import org.example.backend.entity.Animal;
import org.example.backend.entity.AnimalCategory;
import org.example.backend.exception.BusinessException;
import org.example.backend.repository.AnimalRepository;
import org.example.backend.repository.AnimalCategoryRepository;
import org.example.backend.service.AnimalService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AnimalServiceImpl implements AnimalService {

    private final AnimalRepository animalRepository;
    private final AnimalCategoryRepository categoryRepository;

    @Override
    @Transactional
    public Animal createAnimal(AnimalDTO animalDTO) {
        AnimalCategory category = categoryRepository.findById(animalDTO.getCategoryId())
                .orElseThrow(() -> new BusinessException("动物分类不存在"));

        Animal animal = new Animal();
        animal.setName(animalDTO.getName());
        animal.setCategory(category);
        animal.setDescription(animalDTO.getDescription());
        animal.setImageUrl(animalDTO.getImageUrl());
        animal.setLocationX(animalDTO.getLocationX());
        animal.setLocationY(animalDTO.getLocationY());

        return animalRepository.save(animal);
    }

    @Override
    @Transactional
    public Animal updateAnimal(Long id, AnimalDTO animalDTO) {
        Animal animal = animalRepository.findById(id)
                .orElseThrow(() -> new BusinessException("动物不存在"));

        AnimalCategory category = categoryRepository.findById(animalDTO.getCategoryId())
                .orElseThrow(() -> new BusinessException("动物分类不存在"));

        animal.setName(animalDTO.getName());
        animal.setCategory(category);
        animal.setDescription(animalDTO.getDescription());
        animal.setImageUrl(animalDTO.getImageUrl());
        animal.setLocationX(animalDTO.getLocationX());
        animal.setLocationY(animalDTO.getLocationY());

        return animalRepository.save(animal);
    }

    @Override
    @Transactional
    public void deleteAnimal(Long id) {
        if (!animalRepository.existsById(id)) {
            throw new BusinessException("动物不存在");
        }
        animalRepository.deleteById(id);
    }

    @Override
    public Animal getAnimal(Long id) {
        return animalRepository.findById(id)
                .orElseThrow(() -> new BusinessException("动物不存在"));
    }

    @Override
    public Page<Animal> getAnimals(String name, Long categoryId, Pageable pageable) {
        // TODO: 实现动物列表查询，支持按名称和分类过滤
        return animalRepository.findAll(pageable);
    }

    @Override
    public List<Animal> getNearbyAnimals(Double latitude, Double longitude, Double distance) {
        // TODO: 实现附近动物查询
        return animalRepository.findAll();
    }
} 